1 Basic plot functions

2 cheat sheet

![margin cheatsheet](images/plot_margin.PNG)
<img src="images/plot_margin.PNG"/>

reference: www.r-graph-gallery.com

3 Graphic

3.2 Histogram

# Histogram with 5 breakpoints
hist(iris$Sepal.Length, breaks=10)

3.3 Barplot

m <- matrix( c(4,6,4,6,4,6), nrow=2 )
#      [,1] [,2] [,3]
# [1,]    4    4    4
# [2,]    6    6    6
barplot(m, legend=rownames(m), beside=TRUE)

3.4 Boxplot

# Sorting a boxplot based on median value
bymedian <- with(InsectSprays, reorder(spray, -count, median))
boxplot(count ~ bymedian, data = InsectSprays,
          xlab = "spray", ylab = "count", varwidth=TRUE,
          col = "lightgray")

3.5 Rug plot

# both 'density' and rug plot
x <- rexp(200)
plot(density(x))
rug(x)

3.6 Q-Q plot

x <- rnorm(100)
y <- rexp(100)
qqplot(x, y)

3.8 Dendrograms

https://rpubs.com/gaston/dendrograms

http://www.sigmath.es.osaka-u.ac.jp/shimo-lab/prog/pvclust/

hc <- hclust(dist(USArrests[1:7,]), "ave")

#--------- plot 
suppressMessages(library(dendextend))
dend1 <- as.dendrogram(hc)
cutree(dend1,h=70) # it now works on a dendrogram with dendextend library
##     Alabama      Alaska     Arizona    Arkansas  California    Colorado 
##           1           1           1           2           1           2 
## Connecticut 
##           3
dend1 <- color_branches(dend1, k = 4)
dend1 <- color_labels(dend1, k = 5)
plot(dend1)

#--------- plot 
clus5 = cutree(hc, 5)
plot(hc, xlab = ""
     , hang = -1 # set label height
)

rect.hclust(hc, h=70, border="blue")
rect.hclust(hc, h=30, border="red")

legend("topright", "(x,y)"
        , c("Strong relation - red", "Good relation - blue") # add text
        , text.col=c("red", "blue")
        , lty=-1 , pch=NA # set lyt and pch to remove the legend symbols
)

4 Plot configuration

4.1 multiple plots in one graph

# multiple plots in one graph. e.g. 2 rows, 2 columns
par(mfrow=c(2,2),
     mar=c(3,1,3,1), oma=c(0,0,0,0) # margin: buttom left, top, right
     )
     
# switch between plots
p <- par('mfg')) # get the plot 
par(mfg = p)  # focus the plot

4.2 plot with multiple Y scales

x<- 1:10
y1 <- sample(seq(1:50), 10) 
y2 <- sample(seq(100:200), 10) 

# all sides have 2 lines of space margin to give space for axis on the right
par(oma=c(2,2,2,2)) 
# first plot
plot(x=x, y=y1, type="l", col="red") 
# tell R to draw over the first plot
par(new=T) 
# do second plot
plot(x=x, y=y2, type ="l", ylab="", yaxt='n' )  
axis(4, (range(y2)), ylab='y2') # draw second axis on the right
mtext("y2", side=4, line=2.3, adj=0.5, cex=1) # add test label on the right

4.3 rotating axis labels in R

las: 0: always parallel to the axis [default], 1: always horizontal, 2: always perpendicular to the axis, 3: always vertical.

plot(iris$Sepal.Length, las=2)

4.4 label all factor in axis

df <- iris[sample(nrow(iris), 30), ]

plot(df$Petal.Width, xaxt="n", xlab = "" )
axis(1, at=1:nrow(df), labels=df$Species, las=2, cex.axis=0.7)

4.5 fill a stair step function

y <- runif(10, 1, 5)
x <- seq_along(y)
y2 <- rep(y, each=2)[-(length(y)*2)]
x2 <- rep(x, each=2)[-1]
x3 <- c(min(x2), x2, max(x2))
y3 <- c(0, y2, 0)

plot(x, y, pch=16)

polygon(x3, y3, border=NA, col="grey60")
lines(x2, y2)

4.6 plot color

# create a color pattern
cl <- rainbow(5)

# transparent color
library(scales)
col <- alpha("yellow", 0.4)

# use RColorBrewer library
# http://moderndata.plot.ly/create-colorful-graphs-in-r-with-rcolorbrewer-and-plotly/
RColorBrewer::brewer.pal(3, "Set2")

4.7 use legend as text box

legend("topright", "(x,y)"
     , "info text" , title = "title text" # add text
     , lty=-1 , pch=NA # set lyt and pch to remove the legend symbols
     )

5 HTML widget

5.1 highcharter

library(highcharter)
library(magrittr)
library(xts)

x <- rnorm(100, mean=0)
ts <- ts(x)
fXts <- as.xts(ts)

highchart() %>% 
  hc_add_series_xts(fXts, id = "usdjpy") 
# add x axis
highchart() %>% 
  hc_xAxis(categories = as.character(index(fXts))) %>% 
  hc_add_series(name = "sss", data = as.numeric(fXts))

5.2 plotly

library(plotly)
x <- c(1:100)
random_y <- rnorm(100, mean=0)
data <- data.frame(x, random_y)

plot_ly(data, x = ~x, y = ~random_y, type='scatter', mode='lines')

6 Other

Producing Simple Graphs with R : http://www.harding.edu/fmccown/r/

http://personality-project.org/r/r.plottingdates.html

Hmisc http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=Hmisc:Ecdf

heatmap-or-plot-for-a-correlation-matrix http://stackoverflow.com/questions/15887212/heatmap-or-plot-for-a-correlation-matrix

.
^Home Page^